<?php
session_start();


include ("Controle.class.php");

$controle = new Controle();
$conect = MySQL::getInstance();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SiCA - Sistema de Controle de Acesso</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="pages.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery-ui-1.8.11.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script type="text/javascript" src="js/AC_RunActiveContent.js"></script>
<script src="js/jquery121.js" type="text/javascript"></script> 
<script src="js/sound.js" type="text/javascript"></script>
<script src="js/main.js" type="text/javascript"></script>
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-pt-br.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>

<link rel="stylesheet" href="js/lightbox/css/lightbox.css" type="text/css" media="screen" />
<script src="js/lightbox/prototype.js" type="text/javascript"></script>
<script src="js/lightbox/scriptaculous.js?load=effects,builder" type="text/javascript"></script>
<script src="js/lightbox/lightbox.js" type="text/javascript"></script>


<!--BEGIN OF TERMS OF USE. DO NOT EDIT OR DELETE THESE LINES. IF YOU EDIT OR DELETE THESE LINES AN ALERT MESSAGE MAY APPEAR WHEN TEMPLATE WILL BE ONLINE-->
<style>#free-flash-header a,#free-flash-header a:hover {color:#b9b9b9;}#free-flash-header a:hover {text-decoration:none}</style>
<!--END OF TERMS OF USE-->
</head>
<body>																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																	                                                                                                                                                                                         																																																																														                                                                               																																																																										  
<div id="container">
		
  <div id="header">
  	
  	<?php 
		$action = 'login';
		if (isset($_GET['action'])) $action = $_GET['action'];

		switch($action){
			case "login" :
				//if (isset($_GET['error']) && $_GET['error']==1)
					//include ("erro_login.php");
				if(isset($_SESSION['autenticado'])){
					if ($_SESSION['autenticado'] && session_cache_expire()>0)
						header("Location: index.php?action=home");
				}
				include ("login_novo.htm");
				break;

			case "validar" :
				$login = $_POST['matricula'];
				$senha = $_POST['senha'];
				//$senha = md5($senha);
				
				$autenticado = $controle->validar($login, $senha);
				if(!$autenticado){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else{
					$_SESSION['usuario'] = $autenticado;
					$_SESSION['autenticado'] = true;
					header("Location: index.php?action=home");
					exit;
				}
				break;

			case "home" :
				if(!$_SESSION['autenticado']){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else {
					include ("home.php");
					include ("footer.html");
				}
				break;
			
			case "cad_usuario" :
				if(!$_SESSION['autenticado']){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else {
				    include ("home.php");
					include ("cadastro_usuario.htm");
					include ("footer.html");
				}				
				break;
			
			case "relat_eventos" :
				if(!$_SESSION['autenticado']){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else {
					include ("home.php");
					include ("relatorio_eventos.htm");
					include ("footer.html");
				}
				break;
				
			case "eventos_hora" :
				if(!$_SESSION['autenticado']){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else {
					include ("home.php");
					include ("eventos_hora.html");
					include ("footer.html");
				}
				break;
			
			case "eventos_data" :
				if(!$_SESSION['autenticado']){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else {
					include ("home.php");
					include ("eventos_data.html");
					include ("footer.html");
				}
				break;
				
			case "eventos_periodo" :
				if(!$_SESSION['autenticado']){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else {
					include ("home.php");
					include ("eventos_periodo.html");
					include ("footer.html");
				}
				break;

			case "eventos_tipo" :
				if(!$_SESSION['autenticado']){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else {
					include ("home.php");
					include ("eventos_tipo.html");
					include ("footer.html");
				}
				break;

			case "eventos_usuario" :
				if(!$_SESSION['autenticado']){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else {
					include ("home.php");
					include ("eventos_usuario.html");
					include ("footer.html");
				}
				break;
			
			case "cad_evento" :
				if(!$_SESSION['autenticado']){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else {
					include ("home.php");
					include ("cadastro_evento.htm");
					include ("footer.html");
				}
				break;
			
			case "sobre" :
				if(!$_SESSION['autenticado']){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else {
					include ("home.php");
					include ("sobre.htm");
					include ("footer.html");
				}
				break;
				
          case "atualizar" :
                 if(!$_SESSION['autenticado']){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else {

                   $evento = $_POST['nome_evt'];
                   $descricao = $_POST['descricao_evt'];

                   
                   include ("home.php");
                   $query = $conect->insertTypeEvent($evento,$descricao);
                   if($query == 1){
                       include ("cadastro_eventoSucesso.htm");
                   }
                   else{
                         include ("cadastro_usuarioErro.htm");
                         echo "<div style='position: absolute; top:428px; left:220px; color: rgb(255, 0, 0); font-weight: bold; font-size: 16px;'>". "Erro ao salvar no banco: '".$query."'";
                   }
                   include ("footer.html");
				}
				break;
				
          case "atualizarUsuario" :
                 if(!$_SESSION['autenticado']){
					header("Location: index.php?action=login&error=1");
					exit;
				}
				else {

                   $nome = $_POST['nome_usuario'];
                   $matricula = $_POST['matricula'];
                   $email = $_POST['email'];
                   $endereco = $_POST['endereco'];
                   $celular = $_POST['celular'];
                   $telefone = $_POST['residencial'];
                   $senha =  $_POST['senha'];

                   include ("home.php");
                   $query = $conect->insertUser($matricula, $nome, $email, $telefone, $celular, $endereco, '', $senha);
                   if($query == 1){
                       include ("cadastro_usuarioSucesso.htm");
                   }
                   else{
                         include ("cadastro_usuarioErro.htm");
                         echo "<div style='position: absolute; top:428px; left:220px; color: rgb(255, 0, 0); font-weight: bold; font-size: 16px;'>". "Erro ao salvar no banco: '".$query."'";
                   }
                   include ("footer.html");
				}
				break;
			
		}
		?>
